import com.alibaba.fastjson.JSON;
import org.apache.commons.io.FileUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 批量合并缓存并按段重命名视频名称，移动到指定文件夹
 * 适用于有多段.blv的缓存
 * 只需修改文件夹所在目录即可
 */

public class MergeAndRename {

    public static void main(String[] args) {
        String path = "/Users/suxiaomeng/Desktop/download/68559539";    //要遍历的路径,根据文件地址作修改
        File file = new File(path);        //获取其file对象
        func(file);
    }

    private static void func(File file) {

        File[] fs = file.listFiles();

        //配置ffmpeg路径
        FFMpegChangeVideo ffmpeg = new FFMpegChangeVideo("/usr/local/Cellar/ffmpeg/4.2.2/bin/ffmpeg");

        //新的文件路径基础路径
        String newPath = "/Users/suxiaomeng/Study/视频/";

        for (File f : fs) {
            if (f.isDirectory())    //若是目录，则递归打印该目录下的文件
            {
                func(f);
            }
            if (f.getName().contains("entry.json")) {
                String content = null;
                try {
                    content = FileUtils.readFileToString(f, "UTF-8");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //获取原文件路径
                String oldPath = f.getPath().replace("entry.json", "");

                //获取课程名称
                String newDirectoryName = JSON.parseObject(content).getString("title");
                // String newPath = "/Users/suxiaomeng/Study/视频/黑马Java基础+就业班+各种项目idea版本";//视频放的新地址
                newPath += newDirectoryName;

                //判断是否存在该文件夹，不存在则新建
                isChartPathExist(newPath);

                //获取父文件夹名称，用于序列号命名
                String parent = f.getParentFile().getName();

                //读取json文件中该段视频段名称
                String newFileName = JSON.parseObject(content).getJSONObject("page_data").getString("part");
                //String newFileName = parent+"-"+JSON.parseObject(content).getJSONObject("page_data").getString("part") + ".blv";

                //读取视频缓存目录文件夹名称
                String typeTag = JSON.parseObject(content).getString("type_tag");

                if (!isFileExist(oldPath + typeTag + "/filelist.txt")) {
                    //读取缓存目录下所有*.blv并保存为filelist.txt
                    try {
                        ArrayList<String> fileNameList = FileUtil.readFiles1(oldPath + typeTag, new ArrayList<String>());
                        System.out.println(fileNameList.size());
                        for (int i = 0; i < fileNameList.size(); i++) {
                            FileUtil.outputToTxt(("file '" + i + ".blv'"), oldPath + typeTag + "/filelist.txt");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                //获取当前视频的序号
                String no = JSON.parseObject(content).getJSONObject("page_data").getString("page");

                if (Integer.valueOf(no) < 10) {
                    newFileName = "0" + no + "-" + newFileName;
                } else {
                    newFileName = no + "-" + newFileName;
                }

                System.out.println("main.func：新文件路径=" + newPath);
                System.out.println("main.func：父文件夹名称=" + parent);
                System.out.println("main.func：课程名称=" + newDirectoryName);
                System.out.println("main.func：视频缓存目录文件夹名称=" + typeTag);
                System.out.println("main.func：该段视频名称=" + newFileName);

                if (isFileExist(oldPath + typeTag + "/filelist.txt")){
                    try {
                        ffmpeg.convertor2((oldPath + typeTag + "/filelist.txt"),newPath ,newFileName);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            }
        }
    }

    //读取json文件
    public static String readJsonFile(String fileName) {
        String jsonStr = "";
        try {
            File jsonFile = new File(fileName);
            FileReader fileReader = new FileReader(jsonFile);
            Reader reader = new InputStreamReader(new FileInputStream(jsonFile), "utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    //dirPath 文件夹磁盘路径 
    private static void isChartPathExist(String dirPath) {

        File file = new File(dirPath);
        if (!file.exists()) {
            file.mkdirs();
        } else {
            System.out.println(dirPath + " 该文件夹已经存在！");
        }
    }

    //filePath下该文件是否存在
    private static boolean isFileExist(String filePath) {

        File file = new File(filePath);
        return file.exists();
    }

}